/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.representation;

import org.openaecfm.model.representation.GeometricProjectionEnum;
import org.openaecfm.model.representation.GeometricRepresentationContext;
import org.openaecfm.model.representation.GeometricRepresentationSubContext;
import org.openaecfm.service.representation.IGeometricRepresentationSubContextManager;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EntityInstanceNameParameter;
import org.openaecfm.spf.model.EnumerationParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.OmittedParameter;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.RealParameter;
import org.openaecfm.spf.model.SimpleEntityInstance;
import org.openaecfm.spf.model.StringParameter;

public final class GeometricRepresentationSubContextIO extends
		AbstractEntityInstanceInputImpl<GeometricRepresentationSubContext>
		implements IEntityInstanceOutput<GeometricRepresentationSubContext> {
	private IEntityInstanceUtilInput<GeometricRepresentationContext> utilInput;
	private IEntityInstanceUtilOutput<GeometricRepresentationContext> utilOutput;
	private IGeometricRepresentationSubContextManager manager;

	public void setManager(IGeometricRepresentationSubContextManager manager) {
		this.manager = manager;
	}

	public void setUtilOutput(
			IEntityInstanceUtilOutput<GeometricRepresentationContext> utilOutput) {
		this.utilOutput = utilOutput;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String exportSPF(GeometricRepresentationSubContext pojo,
			IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance(
				"IFCGEOMETRICREPRESENTATIONSUBCONTEXT",
				appDataManager.getName(pojo));

		utilOutput.exportSPF(pojo, ei, appDataManager);

		// Set CoordinateSpaceDimension
		OmittedParameter dimension = OmittedParameter.getInstance();
		ei.addParameter(dimension);

		// Set Precision
		OmittedParameter precision = OmittedParameter.getInstance();
		ei.addParameter(precision);

		// Set WorldCoordinateSystem
		OmittedParameter world = OmittedParameter.getInstance();
		ei.addParameter(world);

		// Set TrueNorth
		OmittedParameter truenorth = OmittedParameter.getInstance();
		ei.addParameter(truenorth);

		// Set ParentContext
		EntityInstanceNameParameter parent = new EntityInstanceNameParameter(
				appDataManager.getName(pojo.getParentContext()));
		ei.addParameter(parent);

		// Set TargetScale
		if (pojo.getTargetScale() == null) {
			DollarParameter d = DollarParameter.getInstance();
			ei.addParameter(d);
		} else {
			RealParameter r = new RealParameter(pojo.getTargetScale());
			ei.addParameter(r);
		}

		// Set TargetView
		EnumerationParameter targetview = new EnumerationParameter(
				pojo.getTargetView());
		ei.addParameter(targetview);

		// Set UserDefinedTargetView
		if (pojo.getUserDefinedTargetView() == null) {
			DollarParameter d = DollarParameter.getInstance();
			ei.addParameter(d);
		} else {
			StringParameter s = new StringParameter(
					pojo.getUserDefinedTargetView(), StringParameter.UTF_8);
			ei.addParameter(s);
		}

		return ei.toString();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected GeometricRepresentationSubContext doCreateObject() {
		return new GeometricRepresentationSubContext();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveReferenceParameter(EntityInstance ei,
			GeometricRepresentationSubContext pojo,
			IAppDataManager appDataManager) {
		utilInput.resolveReferenceParameter(ei, pojo, appDataManager);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set ParentContext
		EntityInstanceNameParameter parent = (EntityInstanceNameParameter) sei
				.getParameter(6);
		manager.setParentContext(pojo,
				(GeometricRepresentationContext) appDataManager
						.getObject(parent.toInstanceName()));
	}

	public void setUtilInput(
			IEntityInstanceUtilInput<GeometricRepresentationContext> utilInput) {
		this.utilInput = utilInput;
	}

	@Override
	protected void resolveNotReferenceParameter(EntityInstance ei,
			GeometricRepresentationSubContext pojo) {
		utilInput.resolveNotReferenceParameter(ei, pojo);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set TargetScale
		Parameter target = sei.getParameter(7);
		if (target.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) target;
			pojo.setTargetScale(r.toDouble());
		}

		// Set TargetView
		EnumerationParameter view = (EnumerationParameter) sei.getParameter(8);
		pojo.setTargetView(GeometricProjectionEnum.valueOf(view.toEnumString()));

		// Set UserDefinedTargetView
		Parameter userdtv = sei.getParameter(9);
		if (userdtv.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) userdtv;
			pojo.setUserDefinedTargetView(s.toUTF8());
		}
	}
}
